#include "ain.h"
#include "ui_ain.h"

Ain::Ain(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Ain)
{
    ui->setupUi(this);
}

Ain::~Ain()
{
    delete ui;
}

void Ain::on_Adelete_clicked()
{
    dates=ui->Adate->text();
    dates.chop(1);
    ui->Adate->setText(dates);
}


void Ain::on_Aclean_clicked()
{
    dates=ui->Adate->text();
    dates.clear();
    ui->Adate->clear();
}



void Ain::on_Aconfirm_clicked()
{
    std::string a,part[50];
    double n[50], ping, sum = 0;
    int num = 0;//数据个数
    double bei, biao, bu, t;

    dates=ui->Adate->text();
    a=dates.toStdString();


    for (int i = 0; i < a.length(); i++) {//字符串分成小字符串
        if (a[i] != ' ') {
            part[num] += a[i];
        }
        else {
            num++;
        }
    }
    num+=1;
    for (int i = 0; i < num; i++) {//字符串转为double存在数组中
        n[i] = stod(part[i]);
    }
    for (int i = 0; i < num; i++) {//计算算术平均值
        sum += n[i];
    }
    ping = sum / num;
    double s = 0;
    for (int i = 0; i < num; ++i) {//根据贝塞尔公式
        s += (n[i] - ping) * (n[i] - ping);
    }
    s = s / (num - 1);
    bei = sqrt(s);
    biao = bei / ((double)sqrt(num));//标准偏差
    switch (num-1) {//判断t值
    case 1:
        t = 1.84;
        break;
    case 2:
        t = 1.32;
        break;
    case 3:
        t = 1.20;
        break;
    case 4:
        t = 1.14;
        break;
    case 5:
        t = 1.11;
        break;
    case 6:
        t = 1.09;
        break;
    case 7:
        t = 1.08;
        break;
    case 8:
        t = 1.07;
        break;
    case 9:
        t = 1.06;
    case 10:
        t = 1.05;
        break;
    case 15:
        t = 1.04;
        break;
    case 20:
        t = 1.03;
        break;
    case 30:
        t = 1.02;
        break;
    case 40:
        t = 1.01;
        break;
    default:
        t = 1.00;
        break;
    }
    bu = t * biao;//不确定度
    ui->beisai->setText(QString::number(bei,10,6));
    ui->biaozhun->setText(QString::number(biao,10,6));
    ui->buqueding->setText(QString::number(bu,10,6)) ;
    Aanswer=QString::number(bu,10,4);
}


void Ain::on_Aback_clicked()
{
    this->hide();
    MainWindow *n=new MainWindow;
    n->show();
}


void Ain::on_Acontinue_clicked()
{
    this->hide();
    Hun *m=new Hun;
    m->show();
}

